package com.ytf.test.ninth;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Scanner;

/**
 * @author: YTF
 * @date: 2025/1/28 12:35
 * @version: 1.0
 * @description:
 */
public class AdaptedRandomDouble extends RandomDoubles implements Readable {
    private int count;

    public AdaptedRandomDouble(int count) {
        this.count = count;
    }

    @Override
    public int read(CharBuffer cb) throws IOException {
        if (count-- == 0){
            return -1;
        }
        String result = Double.toString(next())+" ";
        cb.append(result);
        return 0;
    }

    public static void main(String[] args) {
        AdaptedRandomDouble adaptedRandomDouble = new AdaptedRandomDouble(7);
        Scanner scanner = new Scanner(adaptedRandomDouble);
        while (scanner.hasNextDouble()){
            System.out.println(scanner.nextDouble()+" ");
        }
        /**
         * OutPut:
         * 0.7271157860730044
         * 0.5309454508634242
         * 0.16020656493302599
         * 0.18847866977771732
         * 0.5166020801268457
         * 0.2678662084200585
         * 0.2613610344283964
         *
         */
    }
}
